import { _decorator, Component, Game, log, Node, v2, v3, view } from "cc";
import { GameEventType, GameOverType } from "../Event/EnumDefine";
import { EventManager } from "../Event/EventManager";
import Common from "../Config/Common";

const { ccclass, property } = _decorator;

@ccclass
export default class PlayerHeart extends Component {

    @property([Node])
    hearts: Node[] = []; // 存储三张爱心图的节点

    private currentIndex: number = 0; // 当前隐藏的爱心图索引

    onLoad() {
        EventManager.instance.registerEvent(GameEventType.Player_Health_Reduced, this.hideHeart, this);

        this.onScreen();
        EventManager.instance.registerEvent(GameEventType.Screen_Change, this.onScreen, this);
    }

    onScreen() {
        this.node.position = v3(Common.instance.getHeartX(), 0);
    }

    hideHeart() {
        if (this.currentIndex < this.hearts.length) {
            this.hearts[this.currentIndex].active = false; // 隐藏当前爱心图
            this.currentIndex++;

            if (this.currentIndex === this.hearts.length) {
                this.gameOver(); // 当所有爱心图都隐藏后，游戏结束
            }
        }
    }

    gameOver() {
        EventManager.instance.triggerEvent(GameEventType.Game_Over, GameOverType.PlayerDie)
    }
}
